#!/bin/bash

#get this script real directory
SOURCE="$0"
while [ -h "$SOURCE"  ]; do # resolve $SOURCE until the file is no longer a symlink
    this_script_dir="$( cd -P "$( dirname "$SOURCE"  )" && pwd  )"
    SOURCE="$(readlink "$SOURCE")"
    # if $SOURCE was a relative symlink, 
    #we need to resolve it relative to the path 
    #where the symlink file was located
    [[ $SOURCE != /*  ]] && SOURCE="$this_script_dir/$SOURCE"
done
this_script_dir="$( cd -P "$( dirname "$SOURCE"  )" && pwd  )"

_cur_dir=`pwd`


#tarball_dir=$HOME/git/tarball
tarball_dir=$this_script_dir/../dependence
install_dir=$HOME/git/run

source $this_script_dir/insert_profile.sh

###########################################################################33
#install google protobuf
install_protobuf()
{
    _fun_dir_tmp=`pwd`

    echo "-- info,start to install protobuf."
    #protoc --version  | grep -E "libprotoc 3.1.0|3.11.1|3.10.0" > /dev/null
    which protoc && protoc --version  | grep -E "libprotoc 3.11.1|3.10.0" > /dev/null

    if [ $? -ne 0 ] ; then
        echo "-- info, protoc does not exist, will install it now."
        cd $tarball_dir && tar -zxf protobuf-cpp-3.11.1.tar.gz && cd protobuf-3.11.1 && ./configure && make && sudo make install
    else
        echo "-- info, protoc 3.11.1 already exist."

    fi

    cd $_fun_dir_tmp
    unset _fun_dir_tmp
}

install_boost67()
{
    _fun_dir_tmp=`pwd`

    echo "-- info,start to install boost67."

    if [ ! -d /usr/local/include/boost ]; then
        cd $tarball_dir && tar -zxf boost_1_67_0.tar.gz && cd boost_1_67_0 && chmod +x ./bootstrap.sh  \
            && ./bootstrap.sh --with-libraries=system,test     \
            && ./b2 \
            && sudo ./b2 install
    else
        echo "-- info, boost 1.67.0 already exist."
    fi

    cd $_fun_dir_tmp
    unset _fun_dir_tmp
}


install_fmt_v3()
{
    _fun_dir_tmp=`pwd`
    echo "-- info,start to install fmt_v3."

    cd $tarball_dir && tar -zxf fmt_v3.tar.gz && cd fmt_v3/build && cmake .. -DBUILD_SHARED_LIBS=ON -DFMT_DOC=OFF -DFMT_TEST=OFF && sudo make install 
    cmake .. -DBUILD_SHARED_LIBS=OFF -DFMT_DOC=OFF -DFMT_TEST=OFF && sudo make install 

    cd $_fun_dir_tmp
    unset _fun_dir_tmp
}

install_spdlog()
{
    _fun_dir_tmp=`pwd`
    echo "-- info,start to install spdlog."
    cd $tarball_dir && tar -zxf spdlog.tar.gz 
    #&& cd spdlog/build && camke .. -DBUILD_SHARED_LIBS=ON -DBUILD_TESTING=OFF -DSPDLOG_BUILD_TESTING=OFF && sudo make install 
    cd $_fun_dir_tmp
    unset _fun_dir_tmp
}

install_sqlite()
{
    _fun_dir_tmp=`pwd`
    echo "-- info,start to install sqlite_3.24."
    cd $tarball_dir && tar -zxf sqlite-autoconf-3240000.tar.gz && cd sqlite-autoconf-3240000 && ./configure && make && sudo make install 
    cd $_fun_dir_tmp
    unset _fun_dir_tmp
}
install_protobuf
install_boost67
install_fmt_v3
#install_sqlite
#install_spdlog

insert_profile PATH /usr/local/bin 0
insert_profile LD_LIBRARY_PATH /usr/local/lib 0
echo "--info,install dependence OK, execute \"source ~/.profile\" to update environment or just reopen the terminal,then run your program!"




